#include "shm_participant.h"

ShmParticipant::ShmParticipant(int domain_id, std::string qos_name) {

  // Set qos name
  qos_name_ = qos_name;
  p_qos.name(qos_name_);

    p_qos.wire_protocol().builtin.discovery_config.discoveryProtocol = eprosima::fastrtps::rtps::DiscoveryProtocol_t::SIMPLE;
    p_qos.wire_protocol().builtin.discovery_config.use_SIMPLE_EndpointDiscoveryProtocol = true;
    p_qos.wire_protocol().builtin.discovery_config.m_simpleEDP.use_PublicationReaderANDSubscriptionWriter = true;
    p_qos.wire_protocol().builtin.discovery_config.m_simpleEDP.use_PublicationWriterANDSubscriptionReader = true;
    p_qos.wire_protocol().builtin.discovery_config.leaseDuration = eprosima::fastrtps::c_TimeInfinite;

    // Explicit configuration of SharedMem transport
    p_qos.transport().use_builtin_transports = false;

    auto shm_transport = std::make_shared<eprosima::fastdds::rtps::SharedMemTransportDescriptor>();
    shm_transport->segment_size(4 * 1024 * 1024);
    p_qos.transport().user_transports.push_back(shm_transport);

  // Create participant
  domain_id_ = domain_id;
  participant_ =
      eprosima::fastdds::dds::DomainParticipantFactory::get_instance()
          ->create_participant(domain_id, p_qos);

  // Safety check
  if (participant_ == nullptr) {
    std::cout << "Could not create domain participant";
    // return false;
  }

  // Set qos properties
  participant_->set_qos(p_qos);
}

bool ShmParticipant::init(int domain_id, std::string qos_name)
{
    bool retflag = true;
  // Set qos name
  qos_name_ = qos_name;
  p_qos.name(qos_name_);
  
    p_qos.wire_protocol().builtin.discovery_config.discoveryProtocol = eprosima::fastrtps::rtps::DiscoveryProtocol_t::SIMPLE;
    p_qos.wire_protocol().builtin.discovery_config.use_SIMPLE_EndpointDiscoveryProtocol = true;
    p_qos.wire_protocol().builtin.discovery_config.m_simpleEDP.use_PublicationReaderANDSubscriptionWriter = true;
    p_qos.wire_protocol().builtin.discovery_config.m_simpleEDP.use_PublicationWriterANDSubscriptionReader = true;
    p_qos.wire_protocol().builtin.discovery_config.leaseDuration = eprosima::fastrtps::c_TimeInfinite;

    // Explicit configuration of SharedMem transport
    p_qos.transport().use_builtin_transports = false;

    auto shm_transport = std::make_shared<eprosima::fastdds::rtps::SharedMemTransportDescriptor>();
    shm_transport->segment_size(4 * 1024 * 1024);
    p_qos.transport().user_transports.push_back(shm_transport);

  // Create participant
  domain_id_ = domain_id;
  participant_ =
      eprosima::fastdds::dds::DomainParticipantFactory::get_instance()
          ->create_participant(domain_id, p_qos);

  // Safety check
  if (participant_ == nullptr) {
    std::cout << "Could not create domain participant";
    retflag = false;
  }

  // Set qos properties
  participant_->set_qos(p_qos);

  return retflag;

}

ShmParticipant::~ShmParticipant() {
  eprosima::fastdds::dds::DomainParticipantFactory::get_instance()
      ->delete_participant(participant_);
};
